package com.yb.livewykt.ui.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.util.Log
import android.view.Gravity
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.activity.viewModels
import androidx.databinding.DataBindingUtil
import com.yb.livewykt.R
import com.yb.livewykt.databinding.ActivityLivePlayerInputPanelLayoutBinding
import com.yb.livewykt.util.CHAT_ROOM_INPUT_CONNECT
import com.yb.livewykt.util.showToast
import com.yb.livewykt.vm.LiveInputPanelViewModel

class LiveBottomInputPanelActivity : BaseActivity() {

    lateinit var binding: ActivityLivePlayerInputPanelLayoutBinding
    private val viewModel by viewModels<LiveInputPanelViewModel>()

    override fun initView() {
        binding = DataBindingUtil.setContentView(this,
            R.layout.activity_live_player_input_panel_layout)
        binding.lifecycleOwner = this
        binding.data = viewModel
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        window.setGravity(Gravity.BOTTOM)
    }

    override fun initListener() {

        Handler().postDelayed({
            binding.msgConnect.isEnabled = true
            binding.msgConnect.isFocusableInTouchMode = true
            binding.msgConnect.requestFocus()
            val inputManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputManager.showSoftInput(binding.msgConnect,0)

        },200)

        binding.sendMsg.setOnClickListener {
            if (binding.msgConnect.text?.isEmpty()!!){
                resources.getString(R.string.write_something).showToast()
            }else{
                hideInput()
                val intent = Intent()
                Log.d(TAG, "initListener: ${binding.msgConnect.text.toString()}")
                intent.putExtra(CHAT_ROOM_INPUT_CONNECT,viewModel.msgConnect.value)
                setResult(Activity.RESULT_OK,intent)
                finish()
            }
        }
    }


    /**
     * 隐藏键盘
     */
    private fun hideInput() {
        val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        val v = window.peekDecorView()
        if (null != v) {
            imm.hideSoftInputFromWindow(v.windowToken, 0)
        }
    }
}